package com.yourname.bank_sim;

import com.yourname.bank_sim.entity.Account;
import com.yourname.bank_sim.entity.ActionLog;
import com.yourname.bank_sim.list.CustomList;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class DataStore {
    private static CustomList<Account> accounts=new CustomList<>();
    private static CustomList<ActionLog> logs=new CustomList<>();
    public static  void addAccount(Account account){
        accounts.add(account);
    }
    public static  void addActionLog(ActionLog actionLog){
        logs.add(actionLog);
    }
    public static void updateAccount(Account newAccount){
        accounts.updateByAccount(newAccount.getAccountNumber(),newAccount);
    }
    public static void updateActionLog(ActionLog log){
        logs.updateByAccount(log.getActor(),log);
    }
    public static Account getAccountByAccountAndPassword(String account,String password){
        Account objectByAccount = accounts.getObjectByAccountNumber(account, new Account());
        if (objectByAccount!=null){
            if (objectByAccount.getPassword().equals(password)){
                return objectByAccount;
            }else{
                System.out.println("密码不正确");
                return null;
            }
        }else{
            System.out.println("账户不存在");
            return null;
        }
    }

    public static Account getAccountByAccountNumber(String target) {
        return accounts.getObjectByAccountNumber(target,new Account());
    }
    private static void saveAccounts(){
        File f = new File("account.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            StringBuilder printStr = new StringBuilder();
            printStr.append("账号,余额,密码\n");
            for (int i = 0; i < accounts.size(); i++) {
                Account account = accounts.get(i);
                printStr.append(account.getAccountNumber()).append(",").append(account.getBalence()).append(",")
                        .append(account.getPassword()).append("\n");
            }
            FileOutputStream fileOutputStream = new FileOutputStream(f);
            fileOutputStream.write(printStr.toString().getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void loadAccounts(){
        File f = new File("account.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            BufferedReader fileReader=new BufferedReader(new FileReader(f));
            fileReader.readLine();
            while (true){
                String line = fileReader.readLine();
                if (line==null)
                    break;
                String[] split = line.split(",");
                Account account=new Account();
                account.setAccountNumber(split[0]);
                account.setBalence(new BigDecimal(split[1]));
                account.setPassword(split[2]);
                accounts.add(account);
            }
            fileReader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void saveLog(){
        File f = new File("actionLog.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            StringBuilder printStr = new StringBuilder();
            printStr.append("操作时间,账号,金额,余额,操作类型,是否成功\n");
            for (int i = 0; i < logs.size(); i++) {
                ActionLog log = logs.get(i);
                printStr.append(log.getDate()).append(",").append(log.getActor()).append(",")
                        .append(log.getAmount()).append(",").append(log.getBalence()).append(",")
                        .append(log.getActionType()).append(",").append(log.isSuccess()).append("\n");
            }
            FileOutputStream fileOutputStream = new FileOutputStream(f);
            fileOutputStream.write(printStr.toString().getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void loadLogs(){
        File f = new File("actionLog.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            BufferedReader fileReader=new BufferedReader(new FileReader(f));
            fileReader.readLine();
            while (true){
                String line = fileReader.readLine();
                if (line==null)
                    break;
                String[] split = line.split(",");
                ActionLog log=new ActionLog();
                log.setDate(new Date(split[0]));
                log.setActor(split[1]);
                log.setAmount(new BigDecimal(split[2]));
                log.setBalence(new BigDecimal(split[3]));
                log.setActionType(split[4]);
                log.setSuccess(Boolean.parseBoolean(split[5]));
                logs.add(log);
            }
            fileReader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void saveData(){
        saveAccounts();
        saveLog();
    }
    public static void loadData(){
        loadAccounts();
        loadLogs();
    }
    public static List<ActionLog> getLogsByAccountNumber(String an){
        List<ActionLog> myLogs=new ArrayList<>();
        Iterator<ActionLog> iterator = logs.iterator();
        while (iterator.hasNext()){
            ActionLog next = iterator.next();
            if (next.getActor().equals(an)){
                myLogs.add(next);
            }
        }
        return myLogs;
    }
}
